package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.Owner;
import com.example.sprinbootdemo.service.OwnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/owners")
public class OwnerController {

    private static final Logger logger = LoggerFactory.getLogger(OwnerController.class);

    @Autowired
    private OwnerService ownerService;

    // 获取所有的车主记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<Owner> list;
        try {
            list = ownerService.getAllOwners();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取车主记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取车主记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 添加新的车主记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addOwner(@RequestBody Owner owner) {
        try {
            logger.info("添加车主记录: {}", owner);
            ownerService.addOwner(owner);
            ApiResponse response = new ApiResponse(1, "车主记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加车主记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加车主记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的车主记录
    @DeleteMapping("/delete/{ownerId}")
    public ResponseEntity<ApiResponse> deleteOwnerByOwnerId(@PathVariable int ownerId) {
        try {
            logger.info("删除车主记录, OwnerID: {}", ownerId);
            ownerService.deleteOwnerById(ownerId);
            ApiResponse response = new ApiResponse(1, "车主记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除车主记录失败, OwnerID: {}", ownerId, e);
            ApiResponse response = new ApiResponse(0, "删除车主记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的车主记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateOwnerByOwnerId(@RequestBody Owner owner) {
        try {
            logger.info("更新车主记录: {}", owner);
            ownerService.updateOwnerById(owner);
            ApiResponse response = new ApiResponse(1, "车主记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新车主记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新车主记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 根据姓名查询车主记录
    @GetMapping("/searchByName/{name}")
    public ResponseEntity<ApiResponse> getOwnersByName(@PathVariable String name) {
        List<Owner> owners;
        try {
            owners = ownerService.getOwnersByName(name);
            ApiResponse response = new ApiResponse(1, "成功", owners);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("根据姓名查询车主记录失败, Name: {}", name, e);
            ApiResponse response = new ApiResponse(0, "根据姓名查询车主记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}